package hrc.wow_notifier.service.impl;

import hrc.wow_notifier.service.NotifyService;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.util.Log;


public class ChinaServerNotifyService extends NotifyService {
	private static final String CHINESE_SERVER_URL="http://www.warcraftchina.com/action/system/realmStatus.aux";
	private static final String REGEX_FOR_NAME="\"l\":\"\\w+\",\"n\":\"(.+?)\",\"q\":\"\\w+\",\"s\":\"\\w+\",\"t\":\"\\w+[\\s|\\S]\\w+\"";
	private static final String REGEX_FOR_STATUS="\"l\":\"\\w+\",\"n\":\"%s\",\"q\":\"\\w+\",\"s\":\"(.+?)\",\"t\":\"\\w+[\\s|\\S]\\w+\"";
	@Override
	protected List<String> analyseServerName(String result) {
		Pattern p=Pattern.compile(REGEX_FOR_NAME);
		Matcher m=p.matcher(result);
		List<String> re=new ArrayList<String>(360);
		while(m.find()){
			Log.d("test", m.group(1));
			re.add(m.group(1));
		}
		return re;
	}

	@Override
	protected String getServerUrl() {
		return CHINESE_SERVER_URL;
	}

	@Override
	protected boolean analyseServerStatus(String result, String serverName) {
		String regex=String.format(REGEX_FOR_STATUS, serverName);
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(result);
		while (m.find()) {
			return "Up".equals(m.group(1));
		}
		return false;
	}
}
